<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="id?'编辑月卡':'新增月卡'" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="carInfoForm" :model="carInfoForm" :rules="carInfoRules" label-width="100px">
            <el-form-item prop="personName" label="车主姓名">
              <el-input v-model="carInfoForm.personName" placeholder="请输入车主名字" />
            </el-form-item>
            <el-form-item prop="phoneNumber" label="联系方式">
              <el-input v-model="carInfoForm.phoneNumber" placeholder="请输入联系方式" />
            </el-form-item>
            <el-form-item prop="carNumber" label="车辆号码">
              <el-input v-model="carInfoForm.carNumber" placeholder="请输入车辆号码" />
            </el-form-item>
            <el-form-item prop="carBrand" label="车辆品牌">
              <el-input v-model="carInfoForm.carBrand" placeholder="请输入车辆品牌" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="carPayForm" :model="carPayForm" :rules="carPayRules" label-width="100px">
            <el-form-item prop="payTime" label="有效日期">
              <el-date-picker
                v-model="carPayForm.payTime"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item prop="paymentAmount" label="支付金额">
              <el-input v-model="carPayForm.paymentAmount" placeholder="请输入支付金额" />
            </el-form-item>
            <el-form-item prop="paymentMethod" label="支付方式">
              <!-- clearable 下拉框清空功能 -->
              <el-select v-model="carPayForm.paymentMethod" clearable>
                <el-option
                  v-for="item in paymentMethodList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="onCancel">重置</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { validaeCarNumber } from '@/utils/validate'
import { getCardDetailApi, getCreateCardApi, getUpdateCardApi } from '@/api/car'
export default {
  data() {
    return {
      // 车辆信息表单
      carInfoForm: {
        personName: '', // 车主姓名
        phoneNumber: '', // 联系方式
        carNumber: '', // 车牌号码
        carBrand: '' // 车辆品牌
      },
      // 表单校验
      carInfoRules: {
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          // /^1[34578]\d{9}$/ 第一位是1，第二位是34578的其中一个，后面补充九位
          { pattern: /^1[34578]\d{9}$/, message: '请输入正常格式手机号' }
        ],
        carNumber: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          { validator: validaeCarNumber, trigger: 'blur' }
        ],
        carBrand: [
          { required: true, message: '请输入车牌品牌', trigger: 'blur' }
        ]
      },
      // 缴费信息表单
      carPayForm: {
        // cardStartDate: '', // 开始时间
        // cardEndDate: '', // 结束时间
        payTime: [], // 支付时间
        paymentAmount: null, // 支付金额
        paymentMethod: null // 支付方式
      },
      // 支付方式下拉框列表
      paymentMethodList: [
        { id: 'Alipay', name: '支付宝' },
        { id: 'WeChat', name: '微信' },
        { id: 'Cash', name: '线下' }
      ],
      // 缴费信息表单校验
      carPayRules: {
        payTime: [
          { required: true, message: '请输入支付时间' }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请输入支付方式', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    // 缓存id 方便调用
    id() {
      // 从 this.$route.query.id 中获取 id 值并返回
      return this.$route.query.id
    }
  },
  // 检查id存不存在
  mounted() {
    // 如果存在调用getDetail方法
    if (this.id) {
      this.getDetail()
    }
  },
  methods: {
    async onSubmit() {
      // 依次对两个表单进行校验
      await this.$refs.carInfoForm.validate()
      await this.$refs.carPayForm.validate()
      const payload = {
        // 当两个表单通过验证后，将他们的数据合并到payload对象里
        ...this.carInfoForm,
        ...this.carPayForm,
        // 从carPayForm中的payTime数组中提取起始时间和结束时间，分别赋值给cardStartDate和cardEndDate
        cardStartDate: this.carPayForm.payTime[0],
        cardEndDate: this.carPayForm.payTime[1]
      }
      // 删除payload中的payTime字段,因为这个字段在合并数据时已经被处理过了。
      delete payload.payTime

      if (this.id) {
        // 编辑
        await getUpdateCardApi(payload)
      } else {
        // 新增
        await getCreateCardApi(payload)
      }
      // await getCreateCardApi(payload)
      // 返回 router.go(-1)
      this.$router.back()
      if (this.id) {
        this.$message.success('更新月卡成功')
      } else {
        this.$message.success('新增月卡成功')
      }
    },
    onCancel() {
      // el-form 会用默认 v-model 的初始值作为重置的数据
      this.$refs.carPayForm.resetFields()
      this.$refs.carInfoForm.resetFields()
    },
    async getDetail() {
      const res = await getCardDetailApi(this.id)
      // console.log(res)
      // 回填车辆信息表单
      const { carInfoId, personName, phoneNumber, carNumber, carBrand } = res
      this.carInfoForm = {
        personName, phoneNumber, carNumber, carBrand, carInfoId
      }
      // 回填支付信息表单
      const { rechargeId, paymentAmount, paymentMethod, cardStartDate, cardEndDate } = res
      this.carPayForm = {
        rechargeId, paymentAmount, paymentMethod, payTime: [cardStartDate, cardEndDate]
      }
    }
  }
}

</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
